﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using mscook.core;
using mscook.core.domain;
using mscook.core.repositories;
using mscook.mvc.core;
using mscook.core.services;
using mscook.mvc.Core;

namespace mscook.mvc.Areas.weblogin.Controllers
{
    [UserAuthorizeAttribute(Roles = "admin")]
    public class BonusBuyController : BaseController
    {
        //
        // GET: /weblogin/BonusBuy/
        private ILoginUserService iLoginUserService;

        private IBonusBuyRepository iBonusBuyRepository;
        public BonusBuyController(IBonusBuyRepository iBonusBuyRepository, ILoginUserService iLoginUserService)
            : base(iLoginUserService)
        {
            this.iLoginUserService = iLoginUserService;
            this.iBonusBuyRepository = iBonusBuyRepository;
        }

        public ActionResult Index(string nowPageNum, string per = "")
        {
            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<BonusBuy> bonusBuyList = iBonusBuyRepository.SelectPageList(pageHelper.Start, pageHelper.PerValue, out total);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);
            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;
            InitNavBar();
            return View(bonusBuyList);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult add()
        {
            InitNavBar();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult add(BonusBuy bonusBuy)
        {
            if (ModelState.IsValid)
            {
                iBonusBuyRepository.SaveOrUpdate(bonusBuy);

            }
            return RedirectToIndex();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult edit(string id)
        {


            BonusBuy bonusBuy = iBonusBuyRepository.Get(id);
            if (bonusBuy != null)
            {
                InitNavBar();
                return View(bonusBuy);
            }

            return RedirectToIndex();

        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult edit(string Id, BonusBuy bonusBuy)
        {

            if (ModelState.IsValid)
            {
                BonusBuy OldbonusBuy = iBonusBuyRepository.Get(Id);
                UpdateModel(OldbonusBuy);
                iBonusBuyRepository.SaveOrUpdate(OldbonusBuy);


            }
            return RedirectToIndex();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(string id)
        {
            iBonusBuyRepository.PhysicsDelete(id);
            return RedirectToIndex();
        }


    }
}
